<template>
  <el-card>
    <div slot="header" class="clearfix">
      <span>城觅外部订单</span>
    </div>
    <div class="home-container">

      <v-searchForm
        :form-data="filter_info"
        :form-title="formTitle"
        @search="search"
        @reset="reset"
      />

<!--      <v-tableBtns-->
<!--        :table-title="exportTitle"-->
<!--        :table-data="exportData"-->
<!--        :on-export="handleExport"-->
<!--        title="广告订单"-->
<!--      />-->
     <v-tableBtns
        :show-upload="financeRole"
        :table-title="exportTitle"
        :table-data="exportData"
        :on-export="handleExport"
        :upload-url="uploadUrl"
        title="广告订单"
      />

      <v-table
        ref="table"
        :table-title="tableTitle"
        :table-data="tableData"
        @sortChange="sortChange"
        @del="del"
        @edit="edit"
      />
      <v-pagination
        :page-info="pageInfo"
        :get-table-list="getTableList"
      />

    </div>
  </el-card>
</template>

<script>
import { mapGetters } from 'vuex'
import UploadExcelComponent from '@/components/UploadExcel/index.vue'
import {parseTime} from '../../../utils'
import auth from '@/mixins/auth'
  import {$get} from '@/utils/newRequest'
import  AdvOrder  from "../adv_order/index.vue"

export default {
  name: 'Home',
  components: { UploadExcelComponent },

  data() {
    return {
       uploadUrl: process.env.BASE_API + 'action/WechatAdvs/saveFile',
      formTitle: [
        {
          cn: '订单id：',
          en: 'ad_id'
        },
        {
          cn: '广告主：',
          en: 'ad_name'
        },
        {
          cn: '抖音号：',
          en: 'name'
        },
        {
          cn: '商务：',
          en: 'swname',
          placeholder: '输入全称'
        },
        {
          cn: '收款状态：',
          en: 'is_receive',
          type: 'select',
          list: [
            {
              label: '未收款',
              value: 0
            },
            {
              label: '已收款',
              value: 2
            }
          ]
        },
        {
          cn: '是否开票：',
          en: 'is_bill',
          type: 'select',
          list: [
            {
              label: '是',
              value: 1
            },
            {
              label: '否',
              value: 0
            }
          ]
        },
        {
          cn: '发布时间：',
          en: 'created_at',
          type: 'time'
        }
      ],
      filter_info: {},

      tableTitle: [
        {
          cn: 'id',
          en: 'ad_id',
          width: '80px',
          sortable: true
        },
        {
          cn: '抖音号',
          en: 'name',
          sortable: true
        },
        {
          cn: '发布时间',
          en: 'publish_at',
          sortable: true
        },
        {
          cn: '广告主',
          en: 'ad_name'
        },
        {
          cn: '品牌名',
          en: 'brand'
        },
        {
          cn: '金额',
          en: 'ad_price',
          sortable: true
        },
        {
          cn: '微赞收款',
          en: 'real_price',
          sortable: true
        },
        {
          cn: '微萊收款',
          en: 'wl_real_price',
          sortable: true
        },
        {
          cn: '商务',
          en: 'swname',
          sortable: true
        },
        {
          cn: '提交人',
          en: 'fans_name'
        },
        {
          cn: '创建时间',
          en: 'created_at',
          width: '180px',
          sortable: true
        },
        {
          cn: '开票状态',
          en: 'invoice_state'
        }
      ],
      tableData: [],

      sidx: 'publish_at',
      sortKey: null,
      exportTitle: AdvOrder.exp_list,

      exportData: [],
      // f分页
      pageInfo: {
        pageNumber: 1,
        pageSize: 30,
        totalPage: 0,
        totalRow: 0
      }
    }
  },
  activated() {
    this.getTableList()
  },
  methods: {

    getTableList() {
      const loading = this.$loading()
      const datas = {
        ...this.filter_infos,
        page: this.pageInfo.pageNumber, rows: this.pageInfo.pageSize,
        sidx: this.sidx,
        sortKey: this.sortKey,
        type: 2
      }
      this.$get('action/WechatAdvs/dataGrid', datas)
        .then(res => {
          let list
          const data = res.data
          if (data.list && data.list.length) {
            list = data.list.map((item, index) => {
              switch (item.invoice_state) {
                case 0:
                  item.invoice_state = '未开票'
                  break
                case 1:
                  item.invoice_state = '开票中'
                  break
                case 2:
                  item.invoice_state = '已开票'
                  break
              }
              return item
            })
          }

          this.pageInfo.totalPage = data.totalPage
          this.pageInfo.totalRow = data.totalRow
          this.tableData = list || [];
            loading.close()

        })
        .catch(_=>{
          loading.close()
        })
    },

    // 搜索
    search() {
      this.pageInfo.pageNumber=1
      this.getTableList()
    },
    reset() {
      this.filter_info = {}
      this.getTableList()
    },

    leadIn() {
      this.$router.push({
        name: 'adv_order.lead_in'
      })
    },

      del(val) {
        const data = {ad_id: val.row.ad_id}
        this.$post('action/WechatAdvs/del', data)
          .then(res => {
            this.getTableList()
            this.$message({
              message: res.msg,
              type: 'success'
            })
          })
          .catch(_ => {

          })
      },
    // handleExport() {
    //   this.$get('action/WechatAdvs/dataList', {
    //     type: 2,
    //     sidx: 'publish_at',
    //     ...this.filter_infos
    //   })
    //     .then(res => {
    //       const list = res.data.map(item => {
    //         const obj = item
    //         const extend = item.extend && JSON.parse(item.extend)
    //         extend && extend.forEach(item => {
    //           obj[item.label_name] = item.value
    //         })
    //         obj.is_bill = item.is_bill?'是':'否';
    //         obj.is_book = item.is_book?'是':'否';
    //         obj.is_contract = item.is_contract?'是':'否';
    //         return obj
    //       })
    //       this.exportData = list || []
    //     })
    // },

     handleExport() {
        $get('action/WechatAdvs/dataList', {type: 2, sidx: 'publish_at', ...this.filter_infos})
          .then(res => {
            new Promise((resolve, reject) => {
              let datas = res.data;
              let list = datas.map((item, index) => {
                const obj = item
                obj.is_bill = item.is_bill ? '是' : '否';
                obj.is_book = item.is_book ? '是' : '否';
                obj.is_contract = item.is_contract ? '是' : '否';
                obj.return_point = obj.return_point?(obj.return_point+'%'):''
                obj.private_return_point = obj.private_return_point?(obj.private_return_point+'%'):''
                obj.subsidiary_point = obj.subsidiary_point?(obj.subsidiary_point+'%'):''
                if  (item.groups_name == '城觅抖音'){
                 obj.media_type = item.media_type ? '直播' : '短视频';
                 obj.order_form = item.order_form
                 // obj.order_form = item.order_form ? '代下单' : '星图下单';
                }
                else{
                 obj.media_type = '';
                 obj.order_form = '';
                 }
                switch (item.subsidiary_type) {
                  case 0:
                    obj.fenxiao = obj.subsidiary_point;
                    obj.yongjin = '';
                    break;
                  case 1:
                    obj.fenxiao = '';
                    obj.yongjin = obj.subsidiary_point;
                    break;
                  default:
                    obj.fenxiao = '';
                    obj.yongjin = '';
                    break;
                }
                try {
                  const extend = item.extend && JSON.parse(item.extend)
                  extend && extend.forEach(item => {
                    obj[item.label_name] = item.value
                  })
                } catch (e) {
                  //订单号3129返回的extend字段不完整
                  return obj
                }
                return obj
              })
              resolve(list)
            }).then(list => {
              this.exportData = list || []
            }).catch(err => {
              this.exportData = []
            })
          }).catch(_ => {
          this.$message({
            message: '导出超时',
            type: 'error'
          })
          this.exportData = []

        })
      },

    edit(val) {
      console.log(val)
      this.$router.push({
        name: 'adv_order.order_info',
        query: { id: val.row.ad_id }
      })
    },

    addnew() {
      console.log(1)
    },
    // 排序
    sortChange({ column, prop, order }) {
      console.log(column, prop, order)
      this.sidx = prop || 'publish_at'
      this.sortKey = order === 'ascending' ? 'asc' : null
      this.getTableList()
    }

  },
  computed: {
    ...mapGetters([
      'name',
      'roles'
    ]),
    filter_infos() {
      const data = {}
      const origin = this.filter_info
      for (const key in origin) {
        if (!this.filter_info[key] && this.filter_info[key] !== 0) {
          this.$delete(this.filter_info, key)
          continue
        }
        if (key === 'created_at') {
          data.start_time = origin[key][0]
          data.End_time = origin[key][1]
        } else {
          data[key] = origin[key]
        }
      }
      if (!origin.created_at) {
        delete data.start_time
        delete data.End_time
      }
      return data
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
    .table-menu {
        padding-bottom: 15px;
    }

</style>
